package com.swf.seed.security.cert;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * ClassName: SecuritySunSigner <br/>
 * Function: 支持MD5，SHA-1 <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2014-7-2 下午7:39:15 <br/>
 *
 * @author shenwf10476
 * @version 
 * @since JDK 1.6
 */
public class SecuritySunSigner extends SecuritySigner{
	private static final Logger logger = LoggerFactory
			.getLogger(SecuritySunSigner.class);

	public static String encode(String origin, String charset,String algorithm) {
		byte[] orginbytes = null ;
		try {
			orginbytes = charset==null?origin.getBytes():origin.getBytes(charset);
		} catch (UnsupportedEncodingException e) {
			logger.error(e.getMessage(), e);
		} 
		return encode(orginbytes,algorithm);
	}
	
	
	public static String encode(byte[] origin,String algorithm) {
		String resultString = "";
		try {
			MessageDigest md = MessageDigest.getInstance(algorithm);
			resultString = byteArrayToHexString(md.digest(origin));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return resultString;
	}

	private static final String[] hexDigits = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
	
	public static void main(String[] args) throws IOException {
		System.out.println(encode("ABC".getBytes("GBK"), "MD5"));
	}
}
